﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections;

namespace WindowsFormsApplication
{
    public partial class LinqForm : Form
    {
        public LinqForm()
        {
            InitializeComponent();
        }

        private void linqDataTable_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));

            dt.Rows.Add(1, "abc");
            dt.Rows.Add(2, "def");
            dt.Rows.Add(2, "xxx");
            dt.Rows.Add(3, "def");
            dataGridView1.DataSource = dt;

            dt.AsEnumerable().Where<DataRow>(r => r.Field<int>("Id") > 2).AsDataView().ToTable();
        }

        private void IdGreateThan2_Click(object sender, EventArgs e)
        {
            DataTable dt = (dataGridView1.DataSource as DataTable).Copy();
            dataGridView1.DataSource =
            dt.AsEnumerable().Where<DataRow>(r => r.Field<int>("Id") > 2).AsDataView().ToTable();
        }

        private void IQuryableButton_Click(object sender, EventArgs e)
        {
            DataTable dt = (dataGridView1.DataSource as DataTable).Copy();
            IQueryable<DataRow> query = dt.AsEnumerable().AsQueryable().Where<DataRow>(r => r.Field<int>("Id") > 2);

            dataGridView1.DataSource = 
            query.CopyToDataTable<DataRow>();
        }

        private void LinqToObjectButton_Click(object sender, EventArgs e)
        {
            ArrayList arrList = new ArrayList();
            arrList.Add(
                new Student
                {
                    FirstName = "Svetlana",
                    LastName = "Omelchenko",
                    Scores = new int[] { 98, 92, 81, 60 }
                });
            arrList.Add(
                new Student
                {
                    FirstName = "Claire",
                    LastName = "O’Donnell",
                    Scores = new int[] { 75, 84, 91, 39 }
                });
            arrList.Add(
                new Student
                {
                    FirstName = "Sven",
                    LastName = "Mortensen",
                    Scores = new int[] { 88, 94, 65, 91 }
                });
            arrList.Add(
                new Student
                {
                    FirstName = "Cesar",
                    LastName = "Garcia",
                    Scores = new int[] { 97, 89, 85, 82 }
                });
            StringBuilder sb = new StringBuilder();

            foreach (Student s in arrList)
            {
                sb.Append(s.LastName + ": " + s.Scores[0] + "\r\n");
            }

            sb.Append("\r\n\r\n");
            sb.Append("With Query Scores > 95\r\n\r\n");

            var query = from Student student in arrList
                        where student.Scores[0] > 95
                        select student;

            foreach (Student s in query)
               sb.Append(s.LastName + ": " + s.Scores[0]+"\r\n");

            // Keep the console window open in debug mode.
            MessageBox.Show(sb.ToString());
        }

        private void btnDistinct_Click(object sender, EventArgs e)
        {
            try
            {
                List<int> l = new System.Collections.Generic.List<int>();
                DataTable dt = (dataGridView1.DataSource as DataTable).Copy();

                var names = (from DataRow dr in dt.Rows
                             select (int)dr["Id"]).Distinct().OrderBy(name => name);

                foreach (int dr in names)
                {
                    l.Add(dr);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }
    }
}
